package testes;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.util.Collection;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;


import org.jdom.Element;

/**
 * 
 * @author luciano
 */
public class TableModelRegras extends AbstractTableModel {

	private Vector<Regra> regras = new Vector<Regra>();
	private String[] titles = { "Nome", "Filhos" };

	
	public TableModelRegras() {
		
	}
	
	public TableModelRegras(Vector<Regra> regras, String[] titles) {
		this.regras = regras;
		if (titles != null)
			this.titles = titles;
	}

	public void setRegras(Collection<Regra> regras) {
		this.regras.removeAllElements();
		this.regras.addAll(regras);
		fireTableStructureChanged();
	}

	public void setCandidatos(Vector<Regra> regras) {
		this.regras = regras;
		fireTableStructureChanged();
	}

	public Vector<Regra> getRegras() {
		return regras;
	}

	public int getRowCount() {
		return this.regras.size();
	}

	public int getColumnCount() {
		return this.titles.length;
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return (columnIndex == 3) || (columnIndex == 2);
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

		if (columnIndex == 0) {
			if (aValue instanceof Boolean)
				;//regras.get(rowIndex).setEleito((Boolean) aValue);
		}
		fireTableCellUpdated(rowIndex, columnIndex);
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		if (columnIndex == 0)
			return regras.get(rowIndex).getName();
		if (columnIndex == 1){
			String s = "";
			
			for (Object o : regras.get(rowIndex).getChildren()) {
				Element e = (Element) o;
				s += e.getName() + " ";
			}
			
			return s;
		}
		return "";
	}

	public void addRegra(Regra regra) {
		if (!regras.contains(regra))
			regras.add(regra);
		fireTableDataChanged();
	}

	public void remRegra(Regra regra) {
		if (regras.contains(regra))
			regras.remove(regra);
		fireTableDataChanged();
	}

	public Regra getCandidatoAt(int index) {
		return regras.get(index);
	}

	public void remRegraAt(int index) {
		regras.remove(index);
		fireTableDataChanged();
	}

	public void removeAll(int[] sels) {
		for (int i = sels.length - 1; i >= 0; i--) {
			regras.remove(sels[i]);
			fireTableDataChanged();
		}
	}

	@Override
	public Class<?> getColumnClass(int column) {
		return getValueAt(0, column).getClass();
	}

	@Override
	public String getColumnName(int column) {
		return titles[column];
	}

}
